package com.atguigu.maven;

import com.atguigu.maven.mapper.UserMapper;
import com.atguigu.maven.pojo.User;

import com.atguigu.maven.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.*;
import io.minio.errors.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName com.atguigu.maven.TestApplication
 * @Author heartFade
 * @Date 2025-04-23 15:31
 * @Version 1.0
 * @Description TODO
 **/
@SpringBootTest
public class Application {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Test
    public void appTest()
    {
        List<User> users = userMapper.selectList(null);
        System.out.println(users);
    }
   @Test
    public void test2() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//       //1.创建客户端对象，将密码用户名，以及地址传入
//       MinioClient minIo = MinioClient.builder()
//               .credentials("minioadmin","minioadmin")
//               .endpoint("http://192.168.137.88:9000").build();
//      //2.判断是否存在桶
//       boolean isExist = minIo.bucketExists(BucketExistsArgs.builder().bucket("test2").build());
//       if(!isExist)
//       {
//           //2.1.如果不存在就先创建桶
//           minIo.makeBucket(MakeBucketArgs.builder().bucket("test2").build());
//           //2.2给桶设置策略
//           String config= """
//                   {
//                       "Version": "2012-10-17",
//                       "Statement": [
//                           {
//                               "Effect": "Allow",
//                               "Principal": {
//                                   "AWS": [
//                                       "*"
//                                   ]
//                               },
//                               "Action": [
//                                   "s3:GetObject"
//                               ],
//                               "Resource": [
//                                   "arn:aws:s3:::%s/*"
//                               ]
//                           }
//                       ]
//                   }
//                   """.formatted("test2");//给占位符赋值
//           minIo.setBucketPolicy(SetBucketPolicyArgs.builder().config(config).bucket("test2").build());
//
//       }
//       //3.上传文件前先给文件起名
//       String fileName= UUID.randomUUID().toString()+new SimpleDateFormat("yy-MM-dd").format(new Date())+"name.jpg";
//       //4.上传文件
//       minIo.uploadObject(UploadObjectArgs.
//               builder().
//               object(fileName).
//               bucket("test2").
//               filename("F:\\尚庭公寓资料\\资料\\图片资源\\7.images\\7.images\\公寓-外观.jpg").
//               contentType("image/jpg").
//               build());
   }
@Test
public void test6() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    //创建
    MinioClient build = MinioClient.builder().credentials("minioadmin", "minioadmin").endpoint("http://192.168.137.88:9000").build();
    //判断是否存在
    boolean test = build.bucketExists(BucketExistsArgs.builder().bucket("test").build());
    if(!test)
    {
        String aso= """
                {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "AWS": [
                                    "*"
                                ]
                            },
                            "Action": [
                                "s3:GetObject"
                            ],
                            "Resource": [
                                "arn:aws:s3:::%s/*"
                            ]
                        }
                    ]
                }
                """.formatted("test");
        //创建桶
        build.makeBucket(MakeBucketArgs.builder().bucket("test").build());
        //定制策略
        build.setBucketPolicy(SetBucketPolicyArgs.builder().config(aso).bucket("test").build());
    }
    String name=UUID.randomUUID().toString()+"gongyu.jpg";
    build.uploadObject(UploadObjectArgs
            .builder()
            .filename("F:\\\\尚庭公寓资料\\\\资料\\\\图片资源\\\\7.images\\\\7.images\\\\公寓-外观.jpg")
                    .bucket("test")
                    .object(name)
                    .contentType("image/img")
            .build());

}
    @Test
    public void test5(){
//        todo 测试service分页查询
//        1、配置分页插件
//        2、创建分页对象
//        3、查询的时候把分页对象传递给参数

//        返回一个对象,这个对象就是给前端的数据，对象里面包含：总条数  分页数据等等，你想拿到分页数据你得调对象的方法



//        todo 测试mapper分页查询

        Page<User> page = new Page<>(2,3);
        Page<User> page1 = userService.page(page);
        System.out.println(page1.getRecords()); //获得内部数据
        System.out.println();
//        todo 测试自定义分页查询  注意sql当中自己要处理逻辑删除 where deleted = 0;
   List<User> users=userMapper.getUserByPage(page);
        System.out.println(users);
    }


    @Test
    public void test3(){


//        测试lamba条件构造器 解决上述条件构造器的字段硬编码问题 ,后期全部用它

        //查询age介于[20,30]的所有用户
//        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
//        lambdaQueryWrapper.between(User::getAge,20,30);
//        List<User> list = userService.list(lambdaQueryWrapper);
//        System.out.println(list);


//修改张小三为张三
//   LambdaUpdateWrapper<User> lambdaUpdateWrapper=new LambdaUpdateWrapper();
//    lambdaUpdateWrapper.eq(User::getName,"张小三");
//    lambdaUpdateWrapper.set(User::getName,"张三");
//    userService.update(lambdaUpdateWrapper);

        //      todo   测试普通条件构造器  我们只在service当中去测试  因为mapper和service当中的使用方法是一样的


//        todo 查询构造器
        //查询name=赵丽颖的所有用户\
//        1、先实例化条件构造器
//        2、使用条件构造器进行设置条件
//        3、把条件构造器传递给查询方法作为参数
//      LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
//      lambdaQueryWrapper.eq(User::getName,"Jone");
//        System.out.println(userService.getOne(lambdaQueryWrapper));

        //查询邮箱域名为atguigu.com的所有用户
//        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
//      lambdaQueryWrapper.like(User::getEmail,"atguigu");
//        List<User> list = userService.list(lambdaQueryWrapper);
//        System.out.println(list);
        //查询所有用户信息并按照age字段降序排序
//        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
//        lambdaQueryWrapper.orderByDesc(User::getAge);
//        System.out.println(userService.list(lambdaQueryWrapper));


        //查询age小于20或大于30的用户
//        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
//       lambdaQueryWrapper.lt(User::getAge,20).or().gt(User::getAge,30);
//        System.out.println(userService.list(lambdaQueryWrapper));
        //邮箱域名为baomidou.com且年龄小于30或大于40且的用户
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getEmail,"baomidou").and(p->p.gt(User::getAge,20).or().lt(User::getAge,10));
        System.out.println(userService.list(lambdaQueryWrapper));

    }

}
